package yjh.controller;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yjh.pojo.MistakesTopic;
import yjh.service.MistakesTopicService;
import yjh.service.TopicTableService;
import yjh.util.ResultData;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("mistakestopic")
public class MistakesTopicController {
    @Autowired
    MistakesTopicService mistakesTopicService;
    @Autowired
    TopicTableService topicTableService;

    //新增错题
    @PostMapping("/add")
    public Map<String,Object> addMistakesTopic(@RequestParam String topic){
        if (topic==null) return ResultData.fail("输入不合法");

        String s = mistakesTopicService.addMistakesTopic(topic);

        topicTableService.addMid(topic);


        if (s != null) return ResultData.fail(s);

        return ResultData.ok();
    }

    //删除错题
    @DeleteMapping("/delete")
    public Map<String,Object> deleteMistakesTopic(@RequestParam String topic){
        if (topic == null) return ResultData.fail("输入不合法");

        String s = mistakesTopicService.deleteMistakesTopic(topic);
        if (s != null) return ResultData.fail(s);

        return ResultData.ok();
    }

    //更新错题中的错误次数
    @PutMapping("/update")
    public Map<String,Object> update(@RequestParam String topic,
                                     @RequestParam int status){
        if (topic == null) return ResultData.fail("输入有误");

        String s = mistakesTopicService.updateMistakesTopic(topic,status);

        if (s != null) return ResultData.fail(s);

        return ResultData.ok();

    }

    //查询全部错题
    @GetMapping("/selectAll")
    public Map<String,Object> selectAll(){
        List<MistakesTopic> mistakesTopics =
                mistakesTopicService.selectMistakesTopicAll();

        if (mistakesTopics==null) return ResultData.fail("查询失败");

        return ResultData.ok(mistakesTopics);
    }

    // 查询指定重要度的错题 todo
    @GetMapping("/selectByImportanc/{importanc}")
    public Map<String,Object> selectByImportanc(@PathVariable int importanc){
        List<MistakesTopic> mistakesTopics = mistakesTopicService.selectMistakesTopicByImportanc(importanc);

        if (mistakesTopics==null) return ResultData.fail("查询失败");

        return ResultData.ok(mistakesTopics);
    }


    //根据复习时间的范围查询错题
    @GetMapping("/selectMistakesByDate/{date}")
    public Map<String,Object> selectMistakesTopicByDate(@PathVariable int date){
        if (date < 0) {
            return ResultData.fail("数据有误");
        }

        List<MistakesTopic> mistakesTopics =
                mistakesTopicService.selectMistakesTopicByDate(date);

        if (mistakesTopics==null) return ResultData.fail("数据查询失败");

        return ResultData.ok(mistakesTopics);
    }

    // 出错的指定范围内查询
    @GetMapping("/selectMistakesTopicByMistakescount/{count}")
    public Map<String,Object> selectMistakesTopicByMistakescount(@PathVariable int count){
        if (count<0) return null;

        List<MistakesTopic> mistakesTopics =
                mistakesTopicService.selectMistakesTopicByMistakescount(count);

        if (mistakesTopics == null) return ResultData.fail("数据查询失败");

        return ResultData.ok(mistakesTopics);
    }


}
